<?php

/**
 * This is the model class for table "options".
 *
 * The followings are the available columns in table 'options':
 * @property string $id
 * @property string $type
 * @property string $name
 * @property string $value
 * @property integer $input_type
 * @property integer $autoload
 * @property string $tips
 * @property string $sort
 */
class Options extends CActiveRecord
{
    const TYPE_SYSTEM = 0;
    const TYPE_CUSTOM = 1;
    const TYPE_BANNER = 2;
    const TYPE_AD = 3;

    const CUSTOM_AUTOLOAD_NO = 0;
    const CUSTOM_AUTOLOAD_YES = 1;

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{options}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name', 'required'),
			array('name', 'unique'),
			array('input_type, autoload', 'numerical', 'integerOnly'=>true),
			array('type, sort', 'length', 'max'=>11),
			array('name, tips', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, type, name, value, input_type, autoload, tips, sort', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '自增id',
			'type' => '类型.0系统,1自定义,2banner,3广告',
			'name' => '标识符',
			'value' => '值',
			'input_type' => '输入框类型',
			'autoload' => '自动加载.0否,1是',
			'tips' => '提示备注信息',
			'sort' => '排序',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('value',$this->value,true);
		$criteria->compare('input_type',$this->input_type);
		$criteria->compare('autoload',$this->autoload);
		$criteria->compare('tips',$this->tips,true);
		$criteria->compare('sort',$this->sort,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Options the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    /**
     * @return array
     */
    public function getNames()
    {
        return array_keys($this->attributeLabels());
    }

    /**
     * 获取配置信息
     * @param string $var
     * @param string $type
     * @return mixed
     */
    public static function getInfo ($var = '', $type = '0')
    {
        $condition = array ('condition' => 'type=:type' , 'params' => array ('type' => $type ) );
        $model = Options::model()->findAll($condition);
        foreach ($model as $key => $row) {
            if ($var && $var == $row['name']) {
                return $row['value'];
            } else {
                $config[$row['name']] = $row['value'];
            }
        }
        return $config;
    }

    /**
     * @inheritdoc
     */
    /*public function afterSave($insert, $changedAttributes)
    {
        $object = Yii::createObject([
            'class' => FileDependencyHelper::className(),
            'fileName' => 'options.txt',
        ]);
        $object->updateFile();
        parent::afterSave($insert, $changedAttributes);
    }*/

}
